﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Backup.Services.Entities;
using Backup.Services.Logic.Authentification;
using BackupObjects;
using BackupObjects.Taging;

namespace Backup.Services.Logic
{

    /// <summary>
    /// Backup service implementation
    /// </summary>
    public class BackupServiceLogic : IBackupServiceLogic
    {
        private readonly BackupRepository _backupRepository;
        private readonly TagRepository _tagRepository;

        public BackupServiceLogic()
        {
            _backupRepository = new BackupRepository();
            _tagRepository = new TagRepository();
        }

        public BackupEntity LoadById(Guid id, string userName)
        {
            var backupObject = _backupRepository.Load(id, userName);
            BackupEntity backupEntity = backupObject;

            return backupEntity;
        }

        public BackupEntity LoadByName(string name, string userName)
        {
            var backupObject= _backupRepository.Load(name, userName);

            BackupEntity backupEntity = backupObject;

            return backupEntity;
        }

        public List<BackupEntity> LoadAll(string userName)
        {
            _backupRepository.LoadAll(userName);
            var backupEntities=new List<BackupEntity>();
            //Load tags for each entity
            foreach (var backupObject in _backupRepository.Backups)
            {
                BackupEntity backupEntity = backupObject;
                backupEntities.Add(backupEntity);
            } 

            return backupEntities;
        }

        public void Save(BackupEntity backupEntity)
        {
            _backupRepository.Save(backupEntity);
        }

        public void Delete(Guid id, string userName)
        {
            _backupRepository.Delete(id, userName);
        }

        public Stream GetBackupStream(Guid id, string userName)
        {
            return _backupRepository.GetBackupStream(id, userName);
        }

        public List<BackupEntity> GetBackupEntitiesByTags(List<Guid> tagids)
        {
            return _backupRepository.GetBackupEntitiesByTags(tagids).ConvertAll<BackupEntity>(item=>item);
        }

        public List<BackupEntity> GetBackupEntitiesByTag(Guid tagid)
        {
            return _backupRepository.GetBackupEntitiesByTag(tagid).ConvertAll<BackupEntity>(item => item);
        }
    }
}
